using UnityEngine;
using QFramework;
using ProjectSurvivor;
using Unity.Collections.LowLevel.Unsafe;
using System.Collections.Generic;

namespace QFrameworkSurvivor
{
    public partial class RotateSword : ViewController
    {
        private List<Collider2D> mSwords = new List<Collider2D>();
        void Start()
        {
            // Code Here
            void CreatSword()
            {
                mSwords.Add(Sword.InstantiateWithParent(this)
                       .Self(self =>
                       {
                           self.OnTriggerEnter2DEvent(collider =>
                           {
                               var hurtBox = collider.GetComponent<HitHurtBox>();
                               if (hurtBox)
                               {
                                   if (hurtBox.Owner.CompareTag("Enemy"))
                                   {
                                       var damageTimes = Global.SuperRotateSword.Value ? Random.Range(2, 3 + 1) : 1;

                                       DamageSystem.CalculateDamage(Global.RotateSwordDamage.Value * damageTimes, hurtBox.Owner.GetComponent<IEnemy>());
                                       if (hurtBox.Owner.GetComponent<Enemy>() != null && Random.Range(0, 1.0f) < 0.5f)
                                       {
                                           collider.attachedRigidbody.velocity = collider.NormalizedDirection2DFrom(self) * 5 + collider.NormalizedDirection2DFrom(Player.Default) * 10;
                                       }
                                   }
                               }
                           }).UnRegisterWhenGameObjectDestroyed(self);
                       })
                       .Show());
            }
            void CreatSwords()
            {
                var toAddCount = Global.RotateSwordCount.Value + Global.AdditionalFlyThingCount.Value - mSwords.Count;
                for (var i = 0; i < toAddCount; i++)
                {
                    CreatSword();
                }
                UpdateCirclePos();
            }

            Global.RotateSwordCount.Or(Global.AdditionalFlyThingCount).Register(() =>
            {
                CreatSwords();
                if (Global.SuperRotateSword.Value)
                {
                    foreach (var item in mSwords)
                    {
                        item.LocalScale(2);
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            CreatSwords();

            Global.SuperRotateSword.RegisterWithInitValue( super => 
            {
                if(super)
                {
                    foreach (var item in mSwords)
                    {
                        item.LocalScale(2);
                    }
                }
            });

            Global.RotateSwordRange.Register(range =>
            {
                UpdateCirclePos();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

        }
        void UpdateCirclePos()
        {
            var radius = Global.RotateSwordRange.Value;
            var durationDegrees = 360 / mSwords.Count;
            for (var i = 0; i < mSwords.Count; i++)
            {
                var circleLocalPos = new Vector2(Mathf.Cos(durationDegrees * i * Mathf.Deg2Rad),
                                         Mathf.Sin(durationDegrees * i * Mathf.Deg2Rad)) *
                                     radius;
                mSwords[i].LocalPosition(circleLocalPos.x, circleLocalPos.y)
                    .LocalEulerAnglesZ(durationDegrees * i - 90);
            }
        }
        private void Update()
        {
            var speed = Global.SuperRotateSword.Value ? Time.frameCount * 10 : Time.frameCount * Global.RotateSwordSpeed.Value;
            this.LocalEulerAnglesZ(-speed);


        }
    }
}
